#ifndef __LOG_PRINTF_H__
#define __LOG_PRINTF_H__

#include <stdio.h>
#include <stdint.h>

// ANSI颜色控制码
#define LOG_COLOR_RED     "\x1B[1;31m"
#define LOG_COLOR_YELLOW  "\x1B[1;33m"
#define LOG_COLOR_GREEN   "\x1B[1;32m"
#define LOG_COLOR_CYAN    "\x1B[1;36m"
#define LOG_COLOR_RESET   "\x1B[0m"

// 日志级别定义
typedef enum {
    LOG_LEVEL_NONE   = 0,
    LOG_LEVEL_ERROR  = 1,
    LOG_LEVEL_WARN   = 2,
    LOG_LEVEL_INFO   = 3,
    LOG_LEVEL_DEBUG  = 4
} LogLevel;

// 全局配置
#define LOG_ENABLE        1
#define CURRENT_LOG_LEVEL LOG_LEVEL_DEBUG

// 通用日志宏
#if LOG_ENABLE
    #define LOG(level, color, tag, format, ...) \
        do { \
            if ((level) <= CURRENT_LOG_LEVEL) { \
                printf(color tag "[%s:%d][%s] " format LOG_COLOR_RESET "\n", \
                       __FILE__, __LINE__, __func__, ##__VA_ARGS__); \
            } \
        } while(0)
#else
    #define LOG(level, color, tag, format, ...)
#endif

// 分级日志宏
#define log_error(format, ...) \
    LOG(LOG_LEVEL_ERROR, LOG_COLOR_RED, "[E]", format, ##__VA_ARGS__)

#define log_warn(format, ...) \
    LOG(LOG_LEVEL_WARN, LOG_COLOR_YELLOW, "[W]", format, ##__VA_ARGS__)

#define log_info(format, ...) \
    LOG(LOG_LEVEL_INFO, LOG_COLOR_GREEN, "[I]", format, ##__VA_ARGS__)

#define log_debug(format, ...) \
    LOG(LOG_LEVEL_DEBUG, LOG_COLOR_CYAN, "[D]", format, ##__VA_ARGS__)

// 安全数组打印
#define print_array(buffer, len) \
    do { \
        if ((buffer) && (len) > 0) { \
            printf(LOG_COLOR_GREEN); \
            for (int i = 0; i < (len); i++) \
                printf("0x%02X ", ((uint8_t*)(buffer))[i]); \
            printf(LOG_COLOR_RESET "\n"); \
        } \
    } while(0)

#endif // LOG_H